// 导入compression-webpack-plugin
const CompressionWebpackPlugin = require("compression-webpack-plugin");
const UglifyJsPlugin = require("uglifyjs-webpack-plugin");

// 定义压缩文件类型
const productionGzipExtensions = ["js", "css"];
const path = require("path");

function resolve(dir) {
    return path.join(__dirname, dir);
}
module.exports = {
    runtimeCompiler: true, //是否使用包含运行时编译器的 Vue 构建版本
    // 取消eslint警告
    lintOnSave: true,
    // 是否为生产环境构建生成 source map？
    productionSourceMap: false,
    publicPath: process.env.NODE_ENV === "production" ? "./" : "",
    // 输出文件目录
    outputDir: "./dist",
    chainWebpack: config => {
        config.resolve.alias
            .set("@", resolve("src"))
            .set("assets", resolve("src/assets"))
            .set("components", resolve("src/components"))
            .set("layout", resolve("src/layout"))
            .set("base", resolve("src/base"))
            .set("static", resolve("static"));
    },

    configureWebpack: config => {
        if (process.env.NODE_ENV === "production") {
            // 生产环境
            let plugins = [
                new UglifyJsPlugin({
                    uglifyOptions: {
                        compress: {
                            // warnings: false,
                            drop_console: true,
                            drop_debugger: true,
                            pure_funcs: ["console.log"]
                        },
                        output: {
                            // 去掉注释内容
                            comments: false
                        }
                    },
                    sourceMap: false,
                    parallel: require("os").cpus().length > 1
                }),
                new CompressionWebpackPlugin({
                    filename: "[path].gz[query]",
                    algorithm: "gzip",
                    test: new RegExp("\\.(" + productionGzipExtensions.join("|") + ")$"),
                    threshold: 10240,
                    minRatio: 0.8,
                    deleteOriginalAssets: false //是否删除源文件
                })
            ];
            const splitChunksConfig = config.optimization.splitChunks;
            if (config.entry && config.entry instanceof Array) {
                config.entry = {
                    main: config.entry,
                    vendor: [
                        "vue",
                        "vuex",
                        "vue-router",
                    ]
                };
            } else if (config.entry && typeof config.entry === "object") {
                config.entry.vendor = [
                    "vue",
                    "vuex",
                    "vue-router",
                ];
            }

            Object.assign(splitChunksConfig, {
                chunks: "all",
                cacheGroups: {
                    vendors: {
                        test: /node_modules/,
                        name: "vendors",
                        priority: -10
                    },
                    common: {
                        name: "common",
                        minChunks: 2,
                        minSize: 30000,
                        chunks: "all"
                    }
                }
            });
            config.plugins = [...config.plugins, ...plugins];
        } else {
            // 开发环境
        }
    }
};